// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import Qs from 'qs'

import { AjaxPlugin } from 'vux'


import C from  './assets/config'




FastClick.attach(document.body)
Vue.config.productionTip = false
Vue.use(AjaxPlugin);
Vue.use(Vuex)

require('es6-promise').polyfill();

let store = new Vuex.Store();
store.registerModule('vux', {
    state:{
        demoScrollTop: 0,
        isLoading: false,
        direction: 'forward'
    },
    mutations:{
        updateLoadingStatus(state, payload) {
            state.isLoading = payload.isLoading;
        },
        updateDirection(state, payload){
            state.direction = payload.direction
        }
    }
})

// let store = new Vuex.Store();

const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0);
let isPush = false
let endTime = Date.now()
let methods = ['push', 'go', 'replace', 'forward', 'back']
document.addEventListener('touchend', () => {
  endTime = Date.now()
})
methods.forEach(key => {
  let method = router[key].bind(router)
  router[key] = function (...args) {
    isPush = true
    method.apply(null, args)
  }
})
router.beforeEach(function (to, from, next) {
    store.commit('updateLoadingStatus', {isLoading: true});
    const toIndex = history.getItem(to.path)
      const fromIndex = history.getItem(from.path)

      if (toIndex) {
        if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
          store.commit('updateDirection', {direction: 'forward'})
        } else {
          // 判断是否是ios左滑返回
          if (!isPush && (Date.now() - endTime) < 377) {
            store.commit('updateDirection', {direction: ''})
          } else {
            store.commit('updateDirection', { direction: 'reverse' })
          }
        }
      } else {
        ++historyCount
        history.setItem('count', historyCount)
        to.path !== '/' && history.setItem(to.path, historyCount)
        store.commit('updateDirection', {direction: 'forward'})
      }

    next()
});

router.afterEach(function (to) {
  store.commit('updateLoadingStatus', {isLoading: false})
})






Vue.prototype.Query = function(url, send){
    send = send || {};
    var data = Qs.stringify(send);
    store.commit('updateLoadingStatus', {isLoading: true});
    return new Promise((reslove, reject) => {
        this.$http.post('/mobile/'+url, data, {headers:{'Content-Type':'application/x-www-form-urlencoded'}}).then(result=>{
            store.commit('updateLoadingStatus', {isLoading: false});
            let body = result.data;
            store.state.vux.isLoading && store.commit('updateLoadingStatus', {isLoading: false});
            if( body[C.ajax.statusField] == C.ajax.statusCode ){
                reslove(body.body) ;
            }else{
                reject(body);
            }
        }).catch(os =>{
            store.commit('updateLoadingStatus', {isLoading: false})
            alert('错误');
        });
    });
}

/* eslint-disable no-new */
new Vue({
    store,
    router,
    render: h => h(App)
}).$mount('#app-box')
